<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: Email.class.php 145 2012-04-27 09:26:00Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class Email {
	
	public static $config = array ();
	
	static public function init() {
		
		self::$config ['SMTP_HOST'] = C ( 'smtpHost' ) ? C ( 'smtpHost' ) : 'smtp.126.com'; 			//smtp服务器地址
		self::$config ['SMTP_PORT'] = C ( 'smtpPort' ) ? C ( 'smtpPort' ) : 25; 						//smtp服务器端口
		self::$config ['SMTP_SSL'] = C ( 'smtpSecure' ) ? C ( 'smtpSecure' ) : false; 					//是否启用SSL安全连接，gmail需要启用sll安全连接
		self::$config ['SMTP_USERNAME'] = C ( 'smtpUser' ) ? C ( 'smtpUser' ) : 'thinkblog@126.com'; 	//smtp服务器帐号
		self::$config ['SMTP_PASSWORD'] = C ( 'smtpPass' ) ? C ( 'smtpPass' ) : '0f1fb8867b932802'; 	//smtp服务器帐号密码
		self::$config ['SMTP_AUTH'] = C ( 'smtpAuth' ) ? C ( 'smtpAuth' ) : true; 						//启用SMTP验证功能，一般需要开启
		self::$config ['SMTP_CHARSET'] = C ( 'smtpCharset' ) ? C ( 'smtpCharset' ) : 'utf-8'; 			//发送的邮件内容编码	
		self::$config ['SMTP_FROM_TO'] = C ( 'smtpFromTo' ) ? C ( 'smtpFromTo' ) : 'thinkblog@126.com'; //发件人邮件地址
		self::$config ['SMTP_FROM_NAME'] = C ( 'smtpFromName' ) ? C ( 'smtpFromName' ) : 'ThinkBLOG'; 	//发件人姓名
		self::$config ['SMTP_DEBUG'] = C ( 'smtpDEBUG' ) ? C ( 'smtpDEBUG' ) : false; 					//是否显示调试信息	
	}
	
	/**
	 * 邮件发送
	 *
	 * @param: $email[string or array]		接收人邮件地址
	 * @param: $subject[string]     		邮件标题
	 * @param: $content[string]     		邮件内容
	 * @param: $type[int]					0 普通邮件， 1 HTML邮件
	 * @param: $attach[string]				附件
	 *
	 * @return boolean
	 */
	static public function send($email, $subject, $content, $type = 0, $attach = NULL) {
		/**
		 * 使用mail函数发送邮件
		 */
		if (C ( 'mailService' ) == 0 && function_exists ( 'mail' )) {
			/* 邮件的头部信息 */
			$content_type = ($type == 0) ? 'Content-Type: text/plain; charset=' . self::$config ['SMTP_CHARSET'] : 'Content-Type: text/html; charset=' . self::$config ['SMTP_CHARSET'];
			$headers = array ();
			$headers [] = 'From: "' . '=?' . self::$config ['SMTP_CHARSET'] . '?B?' . base64_encode ( self::$config ['SMTP_FROM_NAME'] ) . '?=' . '" <' . self::$config ['SMTP_FROM_TO'] . '>';
			$headers [] = $content_type . '; format=flowed';
			$res = @mail ( $email, '=?' . self::$config ['SMTP_CHARSET'] . '?B?' . base64_encode ( $subject ) . '?=', $content, implode ( "\r\n", $headers ) );
			if (! $res) {
				if (self::$config ['SMTP_DEBUG']) {
					echo "Mailer Send Error";
				}
				return false;
			} else {
				return true;
			}
		} else {
			/**
			 * 使用smtp服务发送邮件
			 */
			@error_reporting ( E_ERROR | E_WARNING | E_PARSE ); //屏蔽出错信息
			import ( '@.ORG.Phpmailer' );
			$mail = new PHPMailer ();
			//没有调用配置方法，则调用一次config方法
			if (! isset ( self::$config ) || empty ( self::$config )) {
				self::init ();
			}
			$mail->IsSMTP ();
			$mail->Host = self::$config ['SMTP_HOST'];
			$mail->Port = self::$config ['SMTP_PORT'];
			$mail->Username = self::$config ['SMTP_USERNAME'];
			$mail->Password = self::$config ['SMTP_PASSWORD'];
			$mail->SMTPAuth = self::$config ['SMTP_AUTH'];
			$mail->CharSet = self::$config ['SMTP_CHARSET'];
			$mail->SetFrom ( self::$config ['SMTP_FROM_TO'], self::$config ['SMTP_FROM_NAME'] );
			$mail->AddReplyTo ( self::$config ['SMTP_FROM_TO'], self::$config ['SMTP_FROM_NAME'] );
			
			//($type == 0) ? $mail->IsHTML ( false ) : $mail->IsHTML ( true );
			//是否启用SSL安全连接	
			if (self::$config ['smtpSecure']) {
				$mail->SMTPSecure = self::$config ['smtpSecure']; //gmail需要启用sll安全连接
			}
			//开启调试信息
			if (self::$config ['SMTP_DEBUG']) {
				$mail->SMTPDebug = 1;
			}
			
			$mail->Subject = $subject;
			$mail->MsgHTML ( $content ); //邮件内容，支持html代码
			//发送邮件
			if (is_array ( $email )) {
				foreach ( $email as $key => $value ) {
					$mail->AddAddress ( $value, "" );
				
				}
			} else {
				$mail->AddAddress ( $email, "" );
			}
			
			//发送多个附件
			if (is_array ( $attach )) {
				foreach ( $attach as $value ) {
					if (file_exists ( $value )) {
						$mail->AddAttachment ( $value );
					}
				}
			}
			//发送一个附件
			if (! empty ( $attach ) && is_string ( $attach )) {
				if (file_exists ( $attach )) {
					$mail->AddAttachment ( $attach );
				}
			}
			if (! $mail->Send ()) {
				if (self::$config ['SMTP_DEBUG']) {
					echo "Mailer Error: " . $mail->ErrorInfo;
				}
				return false;
				continue;
			} else {
				return true;
			}
			// Clear all addresses and attachments for next loop   
			$mail->ClearAddresses ();
			$mail->ClearAttachments ();
		}
	}

}